#include <immintrin.h>
#include <math.h>

// 激活函数类型枚举
typedef enum
{
    RELU,
    SIGMOID,
    TAHN
} act_type_t;

// 通用激活函数宏
#define DEFINE_ACTIVATION(TYPE, T, SUFFIX)   \
    static inline T activation_##SUFFIX(T x) \
    {                                        \
        return TYPE##_IMPL(x);               \
    }

// ReLU实现
#define RELU_IMPL(x) ((x) > 0 ? (x) : 0)
// DEFINE_ACTIVATION(RELU, float, relu_f32)
DEFINE_ACTIVATION(RELU, double, relu_f64)

// Sigmoid实现
#define SIGMOID_IMPL(x) (1.0 / (1.0 + exp(-(x))))
DEFINE_ACTIVATION(SIGMOID, float, sigmoid_f32)
DEFINE_ACTIVATION(SIGMOID, double, sigmoid_f64)

// Tanh实现
#define TANH_IMPL(x) tanh(x)
DEFINE_ACTIVATION(TANH, float, tanh_f32)
DEFINE_ACTIVATION(TANH, double, tanh_f64)

// AVX2优化的ReLU特化，使用宏实现，避免了代码可能无法被inline，直接被展开
#define ACTIVATION_RELU_F32(x) ({ \
    __m128 vec = _mm_set_ss(x); \
    vec = _mm_max_ss(vec, _mm_setzero_ps()); \
    _mm_cvtss_f32(vec); \
})